home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.7 / tcpdump- / tcpdump-richard-1.7 / libpcap-0.0 / pcap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-09  |  4.4 KB  |  201 lines

  1. /*
  2.  * Copyright (c) 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the Computer Systems
  16.  *    Engineering Group at Lawrence Berkeley Laboratory.
  17.  * 4. Neither the name of the University nor of the Laboratory may be used
  18.  *    to endorse or promote products derived from this software without
  19.  *    specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char rcsid[] =
  36.     "@(#) $Header: pcap.c,v 1.12 94/06/12 14:32:23 leres Exp $ (LBL)";
  37. #endif
  38.  
  39. #include <sys/types.h>
  40.  
  41. #include <unistd.h>
  42. #if __STDC__
  43. #include <stdlib.h>
  44. #endif
  45.  
  46. #include "pcap-int.h"
  47.  
  48. int
  49. pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  50. {
  51.     if (p->rf.rfile != NULL)
  52.         return (pcap_read_offline(p, cnt, callback, user));
  53.     else
  54.         return (pcap_read_live(p, cnt, callback, user));
  55. }
  56.  
  57. int
  58. pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  59. {
  60.     for (;;) {
  61.         int n = pcap_dispatch(p, cnt, callback, user);
  62.         if (n < 0)
  63.             return (n);
  64.         if (cnt > 0) {
  65.             cnt -= n;
  66.             if (cnt <= 0)
  67.                 return (0);
  68.         }
  69.         /* return always in case we have been reading from a file */
  70.         if (p->rf.rfile != NULL)
  71.             return (0);
  72.     }
  73. }
  74.  
  75. struct singleton {
  76.     struct pcap_hdr *hdr;
  77.     const u_char *pkt;
  78. };
  79.  
  80.  
  81. static void
  82. pcap_oneshot(u_char *userData, const struct pcap_hdr *h, const u_char *pkt)
  83. {
  84.     struct singleton *sp = (struct singleton *)userData;
  85.     *sp->hdr = *h;
  86.     sp->pkt = pkt;
  87. }
  88.  
  89. const u_char *
  90. pcap_next(pcap_t *p, struct pcap_hdr *h)
  91. {
  92.     struct singleton s;
  93.  
  94.     s.hdr = h;
  95.     if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) < 0)
  96.         return (0);
  97.     return (s.pkt);
  98. }
  99.  
  100. int
  101. pcap_datalink(pcap_t *p)
  102. {
  103.     return (p->linktype);
  104. }
  105.  
  106. int
  107. pcap_snapshot(pcap_t *p)
  108. {
  109.     return (p->snapshot);
  110. }
  111.  
  112. int
  113. pcap_is_swapped(pcap_t *p)
  114. {
  115.     return (p->rf.swapped);
  116. }
  117.  
  118. int
  119. pcap_major_version(pcap_t *p)
  120. {
  121.     return (p->rf.version_major);
  122. }
  123.  
  124. int
  125. pcap_minor_version(pcap_t *p)
  126. {
  127.     return (p->rf.version_minor);
  128. }
  129.  
  130. FILE *
  131. pcap_file(pcap_t *p)
  132. {
  133.     return (p->rf.rfile);
  134. }
  135.  
  136. int
  137. pcap_fileno(pcap_t *p)
  138. {
  139.     return (p->fd);
  140. }
  141.  
  142. void
  143. pcap_perror(pcap_t *p, char *prefix)
  144. {
  145.     fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
  146. }
  147.  
  148. char *
  149. pcap_geterr(pcap_t *p)
  150. {
  151.     return (p->errbuf);
  152. }
  153.  
  154. /*
  155.  * Not all systems have strerror().
  156.  */
  157. char *
  158. pcap_strerror(int errnum)
  159. {
  160.     extern int sys_nerr;
  161.     extern char *sys_errlist[];
  162.     static char ebuf[20];
  163.  
  164.     if ((unsigned int)errnum < sys_nerr)
  165.         return (sys_errlist[errnum]);
  166.     (void)sprintf(ebuf, "Unknown error: %d", errnum);
  167.     return(ebuf);
  168. }
  169.  
  170. int    pcap_setfilter(pcap_t *p, struct bpf_program *bp)
  171. {
  172.     if (p->rf.rfile != NULL)
  173.         return (pcap_setfilter_offline(p, bp));
  174.     else
  175.         return (pcap_setfilter_live(p, bp));
  176. }
  177.  
  178. int pcap_stats(pcap_t *p, struct pcap_stat *ps)
  179. {
  180.     if (p->rf.rfile != NULL)
  181.         return (pcap_stats_offline(p, ps));
  182.     else
  183.         return (pcap_stats_live(p, ps));
  184. }
  185.  
  186. void
  187. pcap_close(pcap_t *p)
  188. {
  189.     /*XXX*/
  190.     if (p->fd >= 0)
  191.         close(p->fd);
  192.     if (p->rf.rfile != NULL) {
  193.         fclose(p->rf.rfile);
  194.         if (p->rf.base != NULL)
  195.             free(p->rf.base);
  196.     } else if (p->buffer != NULL)
  197.         free(p->buffer);
  198.     
  199.     free(p);
  200. }
  201.